package cn.hftoday.org;

import cn.hftoday.db.*;
import junit.framework.TestCase;
import cn.hftoday.util.*;
import cn.hftoday.core.*;

public class RoleTest extends TestCase {

	protected void setUp() throws Exception {
		ApplicationConfig.getInstance().load();
		ConnectionPool.getInstance().open();
		ConnectionPool.getInstance().beginTransaction();
	}

	protected void tearDown() throws Exception {
		ConnectionPool.getInstance().rollback();
		ConnectionPool.getInstance().close();
	}

	public void testGetDescription() {
		Role r = new Role();
		r.setDescription("abcdefghi889");
		assertEquals(r.getDescription(), "abcdefghi889");
	}

	public void testGetBeginTime() {
		Role r = new Role();
		r.setBeginTime(CalendarParser.toCalendar("1900-01-30 15:00:00"));
		assertEquals(r.getBeginTime(), CalendarParser.toCalendar("1900-01-30 15:00:00"));
	}

	public void testGetEndTime() {
		Role r = new Role();
		r.setEndTime(CalendarParser.toCalendar("3208-12-30 15:00:00"));
		assertEquals(r.getEndTime(), CalendarParser.toCalendar("3208-12-30 15:00:00"));
	}
	
	public void testGetRoleType() {
		Role r = new Role();
		r.setRoleType(Role.ROLE_TYPE_COMPANY);
		assertEquals(r.getRoleType(), Role.ROLE_TYPE_COMPANY);
	}
	
	public void testGetCompany() {
		//insert a company instance for test
		Company c = new Company();
		c.setId("c764faf7-8270-46a2-b78c-62959ee48d95");
		c.setName("abcde");
		Mapper.getInstance().insert("InsertCompany", c);
		
		//test person
		Role r = new Role();
		r.setCompany(c);
		
		Company c2 = r.getCompany();
		assertEquals(c2.getId(), "c764faf7-8270-46a2-b78c-62959ee48d95"); 
		assertEquals(c2.getName(), "abcde"); 
	}
	
	public void testSelect() {
		testInsert();
		
		Role r = (Role)Mapper.getInstance().queryForObject("SelectRoleById", "23456788900opqty");
		assertEquals(r.getName(), "huitailang2");
		assertEquals(r.isHidden(), false);
		assertEquals(r.getCreateTime(), CalendarParser.toCalendar("2009-01-14 00:00:00"));
		assertEquals(r.getUpdateTime(), CalendarParser.toCalendar("2009-01-15 00:00:00"));
		assertEquals(r.getDescription(), "miaoshu");
		assertEquals(r.getBeginTime(), CalendarParser.toCalendar("2009-01-20 00:00:00"));
		assertEquals(r.getEndTime(), CalendarParser.toCalendar("3000-01-01 00:00:00"));
		assertEquals(r.getRoleType(), Role.ROLE_TYPE_COMPANY);
	}

	public void testInsert() {
		Role r = new Role();
		r.setId("23456788900opqty");
		r.setName("huitailang2");
		r.setHidden(false);
		r.setCreateTime(CalendarParser.toCalendar("2009-01-14 00:00:00"));
		r.setUpdateTime(CalendarParser.toCalendar("2009-01-15 00:00:00"));
		r.setDescription("miaoshu");
		r.setBeginTime(CalendarParser.toCalendar("2009-01-20 00:00:00"));
		r.setEndTime(CalendarParser.toCalendar("3000-01-01 00:00:00"));
		r.setRoleType(Role.ROLE_TYPE_COMPANY);
		Object o = Mapper.getInstance().insert("InsertRole", r);
		assertSame(r, o);
	}
	
	public void testUpdate() {
		testInsert();
		Role r = (Role)Mapper.getInstance().queryForObject("SelectRoleById", "23456788900opqty");
		
		r.setName("huitailang22");
		r.setHidden(true);
		r.setCreateTime(CalendarParser.toCalendar("2009-01-14 00:00:01"));
		r.setUpdateTime(CalendarParser.toCalendar("2009-01-15 00:00:02"));
		r.setDescription("miaoshu3");
		r.setBeginTime(CalendarParser.toCalendar("2009-01-20 00:00:03"));
		r.setEndTime(CalendarParser.toCalendar("3000-01-01 00:00:04"));
		r.setRoleType(Role.ROLE_TYPE_APPLICATION);
		Mapper.getInstance().update("UpdateRole", r);
		
		r = (Role)Mapper.getInstance().queryForObject("SelectRoleById", "23456788900opqty");
		assertEquals(r.getName(), "huitailang22");
		assertEquals(r.isHidden(), true);
		assertEquals(r.getCreateTime(), CalendarParser.toCalendar("2009-01-14 00:00:01"));
		assertEquals(r.getUpdateTime(), CalendarParser.toCalendar("2009-01-15 00:00:02"));
		assertEquals(r.getDescription(), "miaoshu3");
		assertEquals(r.getBeginTime(), CalendarParser.toCalendar("2009-01-20 00:00:03"));
		assertEquals(r.getEndTime(), CalendarParser.toCalendar("3000-01-01 00:00:04"));
		assertEquals(r.getRoleType(), Role.ROLE_TYPE_APPLICATION);
	}
	
	public void testDelete() {
		testInsert();
		Role r = (Role)Mapper.getInstance().queryForObject("SelectRoleById", "23456788900opqty");
		Mapper.getInstance().delete("DeleteRole", r);
		r = (Role)Mapper.getInstance().queryForObject("SelectRoleById", "23456788900opqty");
		assertEquals(r, null);
	}
}
